/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.behaviours.world;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Timer;

import ca.usherbrooke.agent.world.BasicWorldAgent;
import ca.usherbrooke.behaviours.BasicSubscriptionBehaviour;
import ca.usherbrooke.model.world.IWorldModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class SubscriptionWorldModel extends
		BasicSubscriptionBehaviour<BasicWorldAgent> {
	private static final long serialVersionUID = 1L;

	public static final int SUBSCRIPTION_WORLD_PERFORMATIVE = ACLMessage.SUBSCRIBE;
	public static final String SUBSCRIPTION_WORLD_PROTOCOL = MY_SUBSCRIBE;
	public static final String SUBSCRIPTION_WORLD_MESSAGE = "i subscribe to world model";
	public static final String UNSUBSCRIPTION_WORLD_MESSAGE = "i unsubscribe to world model";

	private static final MessageTemplate TEMPLATE_PERFORMATIVE = MessageTemplate
			.MatchPerformative(SUBSCRIPTION_WORLD_PERFORMATIVE);
	private static final MessageTemplate TEMPLATE_PROTOCOL = MessageTemplate
			.MatchProtocol(SUBSCRIPTION_WORLD_PROTOCOL);
	private static final MessageTemplate TEMPLATE_CONTENT_SUBSCRIBE = MessageTemplate
			.MatchContent(SUBSCRIPTION_WORLD_MESSAGE);
	private static final MessageTemplate TEMPLATE_CONTENT_UNSUBSCRIBE = MessageTemplate
			.MatchContent(UNSUBSCRIPTION_WORLD_MESSAGE);

	private static final MessageTemplate TEMPLATE_RECEIVE_MESSAGE = MessageTemplate
			.and(MessageTemplate.and(TEMPLATE_PERFORMATIVE, TEMPLATE_PROTOCOL),
					TEMPLATE_CONTENT_SUBSCRIBE);

	private static final MessageTemplate TEMPLATE_UNSUBSCRIBE_MESSAGE = MessageTemplate
			.and(MessageTemplate.and(TEMPLATE_PERFORMATIVE, TEMPLATE_PROTOCOL),
					TEMPLATE_CONTENT_UNSUBSCRIBE);

	/**
	 * @param a
	 */
	public SubscriptionWorldModel(BasicWorldAgent a) {
		super(a, TEMPLATE_RECEIVE_MESSAGE, TEMPLATE_UNSUBSCRIBE_MESSAGE);
		Timer t = new Timer(50, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				sendSubscription();
			}
		});
		t.start();
	}

	protected synchronized void sendSubscription() {
		ACLMessage msg = createSubscriptionMessage();
		IWorldModel world = this.getAgent().getWorld();
		try {
			msg.setContentObject(world.copyToTransfert());
			this.sendSubscription(msg);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
